package com.air;

import org.apache.log4j.Logger;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

import java.util.concurrent.TimeUnit;

import static org.testng.Assert.fail;

/**
 * User: Aruna Rajapaksha
 */

public class BasePage extends Base {

    private final Logger logger = Logger.getLogger(getClass());

    protected static WebDriver driver;
    protected String baseUrl;
    protected boolean acceptNextAlert = true;

    @BeforeSuite
    public void beforeSuite() throws Exception {
        try {
            baseUrl = properties.getProperty("APP_URL");
            DELIMITER = properties.getProperty("DELIMITER");
            driver = getWebDriver();
        } catch (Exception e) {
            logger.fatal(e);
            System.exit(0);
        }
    }

    @AfterSuite
    public void afterSuite() throws Exception {
        driver.quit();
    }

    protected WebDriver getWebDriver() {

        int timeout = Integer.parseInt(properties.getProperty("TIME_OUT"));
        String webDriver = properties.getProperty("BROWSER_TYPE");

        if (webDriver == null) {
            logger.debug("BROWSER_TYPE not set. Starting default type");
            driver = new HtmlUnitDriver();
        } else if (webDriver.equalsIgnoreCase("Firefox")) {
            driver = new FirefoxDriver();
        } else if (webDriver.equalsIgnoreCase("Chrome")) {
            driver = new ChromeDriver();
        } else if (webDriver.equalsIgnoreCase("InternetExplorer")) {
            driver = new InternetExplorerDriver();
        } else if (webDriver.equalsIgnoreCase("HtmlUnit")) {
            driver = new HtmlUnitDriver();
        } else {
            logger.debug("BROWSER_TYPE not identified. Starting default type");
            driver = new HtmlUnitDriver();
        }
        logger.debug("Driver Type: " + webDriver);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
        return driver;
    }

    protected void clearAndType(By by, String text) {
        try {
            WebElement field = driver.findElement(by);
            field.clear();
            field.sendKeys(text);
        } catch (Error e) {
            fail(e.toString());
        }
    }

    protected void click(By by) {
        try {
            driver.findElement(by).click();
        } catch (Error e) {
            fail(e.toString());
        }
    }

    protected void select(By by, String visibleText) {
        try {
            new Select(driver.findElement(by)).selectByVisibleText(visibleText);
        } catch (Error e) {
            fail(e.toString());
        }
    }

    protected void setFilePath(By by, String text) {
        try {
            WebElement field = driver.findElement(by);
            field.sendKeys(System.getProperty("user.dir") + "\\src\\test\\resources\\images\\" + text);
        } catch (Error e) {
            fail(e.toString());
        }
    }

    protected void checkPageTitle(String title) {
        try {
            if (!title.equals(driver.getTitle())) {
                throw new IllegalStateException("This is not the intended page");
            }
        } catch (Error e) {
            fail(e.toString());
        }
    }

    protected String getText(By by) {
        return driver.findElement(by).getText();
    }

    protected boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    protected boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    protected String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }
}
